<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Link;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Area;

class AreaController extends Controller
{
    protected $name = '地区管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }

    public function api()
    {
        $q = request()->get('q');

        return Area::where('parent_id', "$q")->get(['id', \DB::raw('name as text')]);
    }

    public function post()
    {
        $post = (request()->get('post'));
        // \Log::info($post);dd(1);
        foreach ($post as $key => $value) {
            $Area = new Area();
            $Area->id = $key;
            $Area->name = isset($value[0])?$value[0]:'';
            $Area->parent_id = isset($value[1])?$value[1]:'';
            $Area->fname = isset($value[2])?$value[2]:'';
            $Area->save();

        }
        dd(1);
    }
        
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Area::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
                
                $filter->like('name', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                
            
            });
            $grid->column('id', 'ID')->sortable();
            $grid->column('listorder', '排序')->editable();
            
            $grid->column('name', '名称');
            $grid->column('url', '链接');
            
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Area::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('name', '名称')->rules('required|min:3');
            $form->url('url', '链接')->rules('nullable')->help('例如：http://www.anhuinews.com/');
            $form->image('thumb','图片链接')->uniqueName();
            $form->number('listorder', '排序')->help('数字越大排前');
			$form->embeds('content', '内容', function ($form) {
				$form->text('title', '标题');
				$form->text('url', '链接');
				$form->image('thumb', '图片')->uniqueName();
				
				
			});
			
			$form->saving(function(Form $form){
				// $form->ignore(['content']);
				
				
				
				// $form->ignore(['content']);
				// dd($form->content);
				// if($form->content['thumb'] == '')
				// {
					// $form->content['thumb'] = $form->model()->content['thumb'];
				// }
				// dd($form->content);
			});
			
            
        });
    }
}
